package cn.momoshenchi.ware.service.impl;

import cn.momoshenchi.common.utils.R;
import cn.momoshenchi.ware.controller.feign.ProductFeignService;
import cn.momoshenchi.ware.entity.PurchaseDetailEntity;
import cn.momoshenchi.ware.vo.SkuHasStockVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.momoshenchi.common.utils.PageUtils;
import cn.momoshenchi.common.utils.Query;

import cn.momoshenchi.ware.dao.WareSkuDao;
import cn.momoshenchi.ware.entity.WareSkuEntity;
import cn.momoshenchi.ware.service.WareSkuService;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService
{

    @Autowired
    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params)
    {
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if (!StringUtils.isBlank(skuId))
        {
            wrapper.eq("sku_id", skuId);
        }
        String wareId = (String) params.get("wareId");
        if (!StringUtils.isBlank(wareId))
        {
            wrapper.eq("ware_id", wareId);
        }

        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params), wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(PurchaseDetailEntity purchaseDetailEntity)
    {
        int count = count(new QueryWrapper<WareSkuEntity>().eq("sku_id", purchaseDetailEntity.getSkuId())
                .eq("ware_id", purchaseDetailEntity.getWareId()));
        if (count == 0)
        {
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(purchaseDetailEntity.getSkuId());
            wareSkuEntity.setStock(purchaseDetailEntity.getSkuNum());
            wareSkuEntity.setWareId(purchaseDetailEntity.getWareId());
            wareSkuEntity.setStockLocked(0);
            try
            {
                R info = productFeignService.info(purchaseDetailEntity.getSkuId());
                Map<String, Object> data = (Map<String, Object>) info.get("sku_info");
                if (info.getCode() == 0)
                {
                    wareSkuEntity.setSkuName((String) data.get("sku_name"));
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

            save(wareSkuEntity);
        }
        else
        {
            baseMapper.addStock(purchaseDetailEntity);
        }
    }

    /**
     *
     * iakjuifz
     * @param skuIds
     * @return
     */

    @Override
    public List<SkuHasStockVo> hasStock(List<Long> skuIds)
    {
        List<SkuHasStockVo> collect = skuIds.stream().map(sku -> {
            SkuHasStockVo vo = new SkuHasStockVo();
            Long stock = baseMapper.hasStock(sku);
            vo.setSkuId(sku);
            vo.setHasStock(stock != null && stock > 0);
            return vo;

        }).collect(Collectors.toList());
return  collect;

    }

}